<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;
class Typeid extends Common
{
    public function index(){	
        $typedb=Db::name('classify')->order('id asc')->select();
        $typedb=get_tree_option1($typedb,0);
        $this->assign('data',$typedb);
        return $this->fetch();
    }


    public function add(){
        $typedb=Db::name('classify')->order('id asc')->select();

        $typedb=get_tree_option1($typedb,0);
        $this->assign('typeid',$typedb);

		$id=input("id");
        if(request()->isAjax()){
            $post=input('post.');;
            $validate=validate('Typeid');

            if($validate->check($post)){
                if(!$id){ //添加数据
                    $typename=Db::name('classify')->where(['name'=>$post['name'],'prev'=>input('prev')])->find();
                    if($typename){
                        return ['err'=>'栏目名称已存在'];
                    }
                    $db=Db::name('classify')->insert($post);
                    if($db){
                        $data=['ret'=>'添加成功'];
                    }else{
                        $data=['err'=>'添加失败'];
                    } 
                }else{ //修改数据
                    $db=Db::name('classify')->where('id',$id)->update($post);
                    if($db){
                        $data=['ret'=>'修改成功'];
                    }else{
                        $data=['err'=>'修改失败'];
                    }
                }
            }else{
                $data=["err"=>$validate->getError()];
            }
        }else{
            if($id){
                $name=Db::name('classify')->where('id',$id)->find();
                //var_dump($name);die();
                if($name){
                     $this->assign('name',$name);
                }else{
                    $data=['err'=>'id参数错误'];
                }
            }else{
                // if($mychannelId){
                //     $mychannelId=_mychannelName($mychannelId,'templest');
                // }
                // $this->assign('mychannelId',$mychannelId);
            }
            $data=$this->fetch();
        }
        return $data;
    }
    // 显示 隐藏栏目
    public function hide(){
        $id=input('id');
        if(request()->isAjax()){
         
            if($id){
                $arra=['0'=>'隐藏','1'=>'显示'];
                $type='hide';
                $db=Db::name('typeid')->where('id',$id)->value('hide');
                if($db==1){
                    $hide=0;
                }else{
                    $hide=1;
                }
                $db=Db::name('typeid')->where('id',$id)->setField($type,$hide);
                if($db){
                    $data=['ret'=>$arra[$hide].'成功','id'=>$hide,'text'=>$arra[$hide]];
                }else{
                    $data=['err'=>$arra[$hide].'失败'];
                }
            }else{
                $data=['err'=>'ID参数错误'];
            }
        }else{
            $data=['err'=>'AJAX参数错误'];
        }
        return $data;
    }
    //删除栏目
    public function typedel(){
        // return ['err'=>'使用采集文章不建议删除分类，否则会影响到采集文章关联分类'];
        if(request()->isAjax()){
          
            $id=input('id');
            if($id){
                $typeid=Db::name('dymanic')->where('category_id',$id)->find();
                if(!$typeid){
                    $db=Db::name('classify')->delete($id);
                    if($db){
                        $data=['ret'=>'删除成功'];
                    }else{
                        $data=['err'=>'删除失败'];
                    }
                }else{
                    $data=['err'=>'分类下有动态，请先删除分类下动态'];
                }
                 
            }else{
                $data=['err'=>'id参数错误'];
            }
        }else{
            $data=['err'=>'提交参数错误'];
        }
        return $data;
    }
    // 下面跳转链接
    public function add1(){
        $url=url('typeid/add',['mychannel'=>'1','url'=>'-1']);
        $this->redirect($url);
    }
    public function add2(){
        $url=url('typeid/add',['mychannel'=>'2','url'=>'-1']);
        $this->redirect($url);
    }
    public function add3(){
        $url=url('typeid/add',['mychannel'=>'3','url'=>'-1']);
        $this->redirect($url);
    }
    
}
